﻿using System;
using System.Windows.Forms;
using pub.Settings;

namespace pub.GUI
{
    public class LastWindowState
    {
        private int _left;
        private int _top;
        private int _height;
        private int _width;
        private FormWindowState _state;

        private ISettings _settings;
        private Form _form;

        public event EventHandler OnStateLoaded;

        public void FireOnStateLoaded(EventArgs e)
        {
            var handler = OnStateLoaded;
            if (handler != null) handler(this, e);
        }

        public LastWindowState(ISettings settings, Form form)
        {
            _settings = settings;
            _form = form;
            _form.Load += OnFormLoad;
            _form.ResizeEnd += _form_ResizeEnd;
            _form.FormClosed += OnFormClosed;
        }

        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            _state = _form.WindowState;
            _settings["left"].IntValue = _left;
            _settings["top"].IntValue = _top;
            _settings["width"].IntValue = _width;
            _settings["height"].IntValue = _height;
            _settings["state"].IntValue = (int) _state;
        }

        private void _form_ResizeEnd(object sender, EventArgs e)
        {
            _left = _form.Left;
            _top = _form.Top;
            _width = _form.Width;
            _height = _form.Height;
        }

        private void OnFormLoad(object sender, EventArgs e)
        {
            _left = _settings["left"].GetValue(_form.Left);
            _top = _settings["top"].GetValue(_form.Top);
            _width = _settings["width"].GetValue(_form.Width);
            _height = _settings["height"].GetValue(_form.Height);
            _state = (FormWindowState) _settings["state"].GetValue((int) _form.WindowState);
            if (_state == FormWindowState.Minimized)
            {
                _state = FormWindowState.Normal;
            }
            FitIntoScreen();

            _form.Height = _height;
            _form.Width = _width;
            _form.Top = _top;
            _form.Left = _left;
            _form.WindowState = _state;

            FireOnStateLoaded(EventArgs.Empty);
        }

        public void FitIntoScreen()
        {
            var bounds = Screen.FromHandle(_form.Handle).WorkingArea;
            if (_height > bounds.Height)
            {
                _height = bounds.Height;
            }

            if (_width > bounds.Width)
            {
                _width = bounds.Width;
            }
            if (_top < 0)
            {
                _top = 0;
            }
            else if (_top + _height/2 > bounds.Bottom)
            {
                _top = bounds.Bottom - _height;
            }
            if (_left < 0)
            {
                _left = 0;
            }
            else if (_left + _width/2 > bounds.Right)
            {
                _left = bounds.Right - _width;
            }
        }
    }
}